<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Models\Fee as Fee;
use App\User as User;
use App\Models\Report as Report;
use App\Models\Report_info as Report_info;
use App\Models\Selection as Selection;
use App\models\Project_info as Project_info;
use App\Http\Requests;
use App\Http\Controllers\Controller;
session_start();

class ThrController extends Controller
{

    ###############################教师端主页#####################################
    public function index(){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        $uid=$_SESSION['sid'];
        $user=User::find($uid);
        if(empty($user))
            $user=array();
 
        return view('thr/home',['user'=>$user]);
    }


    #############################发布新项目###############################################

    public function pro_pub(Request $request){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        if(!isset($_POST['submit']))
            return view('thr/pro_pub');
        else
        {
            #创建新项目
            $project=Project_info::newProj($request);
            return view('project/thr_detail',['data'=>$project]);
        }
    }


    #############################确认项目组员######################################
    public function confirm(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }



        if(!isset($_POST['submit'])){
            $data=Selection::where('pid','=',$pid)->get();
            return view('thr/confirm',['data'=>$data]);
        }
        else
        {
            $sids=$request->input('sid');
            $len=count($sids);

            $data='delete from selections where 1 and pid='.$pid;
            for($i=0;$i<$len;$i++)
                $data=$data.' and sid <> '.$sids[$i];
            $data=$data.';';
            $result=DB::delete($data);
            $data=Selection::where('pid','=',$pid)->get();
            return view('/thr/select_group_guide',['data'=>$data]);
        }
    }
    #选择组长并且开通项目
    public function select_group_guide(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        Project_info::setGuide($pid,$request->input('sid'));
        Project_info::confirmProj($pid);
        Project_info::setState($pid,2);                   #等待提交开题报告         
        $data=Project_info::find($pid);
        return view('project/thr_detail',['data'=>$data]);
    }


    ##############################审核开题报告#############################################
    public function confirm_start(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        return $this->confirm_report($request,'thr/confirm_start',0,$pid,4); #等待提交中期检查表
    }

    ##############################审核中期检查表#############################################
    public function confirm_mid(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        return $this->confirm_report($request,'thr/confirm_mid',1,$pid,6); #等待提交结题报告
    }
    ##############################审核结题报告#############################################
    public function confirm_end(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        return $this->confirm_report($request,'thr/confirm_end',2,$pid,8); #等待提交成果跟踪
            
    }
    ##############################审核成果跟踪#############################################
    public function confirm_result(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        return $this->confirm_report($request,'thr/confirm_result',3,$pid,10); #等待提交展板
            
    }
    ##############################审核展板提交#############################################
    public function confirm_panels(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }

        return $this->confirm_report($request,'thr/confirm_panels',4,$pid,12); #项目完成
            
    }

    ##############################修改项目信息###########################################
    public function modify_pro(Request $request,$pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        if(!isset($_POST['submit'])){
            $project=Project_info::find($pid);
            return view('thr/modify_pro',['data'=>$project]);
        }
        else{
            
            Project_info::modify_pro($request,$pid);
            $this->showMessage('成功修改项目。');
            return $this->index(); 
        }
        
    }


    ##############################删除项目信息###########################################
    public function delete_pro($pid){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }

        $project=Project_info::find($pid);
        $project->delete();
        $this->showMessage('成功删除项目。');
        return $this->index();
    }

    #########################修改密码#################################################
    public function modify_pwd(Request $request){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }


        if(!isset($_GET['submit']))
            return view('thr/modify_pwd');
        else{
            #处理表单信息
            $user = User::findOrFail($_SESSION['sid']);

            if (Hash::check($request->input('password'), $user->password)) {
                // 密码匹配...
                $user->fill(['password' => Hash::make($request->new_password)])->save();
                $this->showMessage('成功设置新密码。');
            }
            else{
                $this->showMessage('原密码出错，请重试。');
            }
            return view('thr/modify_pwd');
        }
    }

    #############################修改个人信息##########################################
    public function modify_info(Request $request){
        if(!isset($_SESSION['sid'])){
            return view('/auth/login');
        }
        
		if(!isset($_GET['submit']))
        {
            $data=User::find($_SESSION['sid']);
            return view('thr/modify_info',['data'=>$data]);
        }
    	else
        {
    		#处理表单信息
            $success=User::modify_info($request);
            $data=User::find($_SESSION['sid']);
            if($success==true)
                $this->showMessage('成功修改个人信息。');
            else
                $this->showMessage('修改信息出错，请重试。');
            return view('thr/modify_info',['data'=>$data]);
    	}
    }
    ####################流程控制：回跳函数##############################
    ################参数:显示信息，返回页面#############################
    public function showMessage($show_info){
            echo '<script type="text/javascript">
                    alert("'.$show_info.'");
                </script>';
    }   


    ########################报告审核##################################
    public function confirm_report($request,$page,$report_type,$pid,$sid){

        $report=Report::where('pid','=',$pid)->where('report_type','=',$report_type)->get()[0];
        if(!isset($_POST['submit'])){
            return view($page,['data'=>$report]);
        }
        else
        {
            Report_info::commit($request,$report->rid);
            Report::qualify($report->rid);
            Project_info::setState($pid,$sid);
            return $this->index();
        }
    }

    ########################下载报告##################################
    ######################文件下载####################################
    public function downloads($name,$real){

        $file_dir = app_path().'\storage\uploads\\';    
         echo $file_dir;
        if (!file_exists($file_dir.$name)){
            header("Content-type: text/html; charset=utf-8");
            echo "File not found!";
            exit(); 
        } else {
            $file = fopen($file_dir.$name,"r"); 
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: ".filesize($file_dir . $name));
            Header("Content-Disposition: attachment; filename=".$real);

            ob_clean();  
            flush();  
            readfile($file_dir.$name);  
        }
    }



    public function modify_fees($pid){

    if(!isset($_GET['submit'])){
    $fees=Fee::find($pid);
        if(empty($fees)){
            #没有做过经费预算
            $fees=Fee::init($pid);
        }
        return view('thr/modify_fees',['data'=>$fees]);
        }
    }

    public function fees(Request $request){
        $pid=$request->input('pid');
        $success=Fee::modify($request,$pid);
        $fees=Fee::find($pid);
        if($success)
            $this->showMessage('成功修改预算');
        else
            $this->showMessage('出现故障，请联系工作人员。');
        return  view('thr/modify_fees',['data'=>$fees]);
    }
}
